package com.example.bookmanager_back.dao;

import com.example.bookmanager_back.entity.Borrow;
import com.example.bookmanager_back.entity.vo.response.BookVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {

    @Insert("insert into borrow(sid, bid, time) values(#{sid}, #{bid}, NOW())")
    void addBorrow(int sid, int bid);

    @Insert("insert into book(title, `desc`, price) values(#{title}, #{desc}, #{price})")
    void addBook(String title, String desc, double price);

    @Delete("delete from borrow where id = #{id}")
    void deleteBorrow(int id);

    @Select("SELECT\n" +
            "    b.*,\n" +
            "    CASE\n" +
            "        WHEN br.bid IS NOT NULL THEN true\n" +
            "        ELSE false\n" +
            "    END AS isBorrowed\n" +
            "FROM\n" +
            "    book b\n" +
            "LEFT JOIN\n" +
            "    borrow br\n" +
            "ON\n" +
            "    b.id = br.bid;")
    List<BookVo> getBookVoList();

    @Select("delete from book where id = #{bid}")
    void deleteBook(int bid);

    @Select("select * from borrow, student, book where borrow.bid = book.id and student.id = borrow.sid")
    List<Borrow> getBorrowList();

    @Select("select count(*) from book")
    int getBookCount();

    @Select("select title from book where id = #{id}")
    String getBookNameById(int id);

    @Select("select name from student where id = #{id}")
    String getStudentNameById(int id);
}